#include "register.h"
#include "libc/stdint.h"
#include "driver/interrupt_id.h"
#include "driver/pl031/time.h"


struct pl031{
    uint32_t RTCDR;
    uint32_t RTCMR;
    uint32_t RTCLR;
    uint32_t RTCCR;
    uint32_t RTCIMSC;
    uint32_t RTCRIS;
    uint32_t RTCMIS;
    uint32_t RTCICR;
    uint32_t Reserved1[24];
    uint32_t Reserved2[5];
    uint32_t Reserved3[975];
    uint32_t Reserved4[4];
    uint32_t RTCPeriphID0;
    uint32_t RTCPeriphID1;
    uint32_t RTCPeriphID2;
    uint32_t RTCPeriphID3;
    uint32_t RTCPCellID0;
    uint32_t RTCPCellID1;
    uint32_t RTCPCellID2;
    uint32_t RTCPCellID3;
}pl031;



struct pl031* pl031_reg_map = (struct pl031*)0x9010000;


void get_current_time(time *time){
    uint32_t time_s = pl031_reg_map->RTCDR;
    bbt_localtime(time, time_s);
}


void pl031_init(){
    time ti = {0};
    get_current_time(&ti);
    printk("\n Time: %d-%d-%d %d:%d:%d \n", ti.year, ti.month, ti.day, ti.hour, ti.minute, ti.second);
    // pl031_print_map();
}






void pl031_print_map(){
    printk("------ print  pl031 -----\n");
    printk("RTCDR: %x(%b)\n", & pl031_reg_map->RTCDR,  pl031_reg_map->RTCDR);
    printk("RTCMR: %x(%b)\n", & pl031_reg_map->RTCMR,  pl031_reg_map->RTCMR);
    printk("RTCLR: %x(%b)\n", & pl031_reg_map->RTCLR,  pl031_reg_map->RTCLR);
    printk("RTCCR: %x(%b)\n", & pl031_reg_map->RTCCR,  pl031_reg_map->RTCCR);
    printk("RTCIMSC: %x(%b)\n", & pl031_reg_map->RTCIMSC,  pl031_reg_map->RTCIMSC);
    printk("RTCRIS: %x(%b)\n", & pl031_reg_map->RTCRIS,  pl031_reg_map->RTCRIS);
    printk("RTCMIS: %x(%b)\n", & pl031_reg_map->RTCMIS,  pl031_reg_map->RTCMIS);
    printk("RTCICR: %x(%b)\n", & pl031_reg_map->RTCICR,  pl031_reg_map->RTCICR);
    printk("Reserved1: %x(%b)\n", & pl031_reg_map->Reserved1,  pl031_reg_map->Reserved1);
    printk("Reserved2: %x(%b)\n", & pl031_reg_map->Reserved2,  pl031_reg_map->Reserved2);
    printk("Reserved3: %x(%b)\n", & pl031_reg_map->Reserved3,  pl031_reg_map->Reserved3);
    printk("Reserved4: %x(%b)\n", & pl031_reg_map->Reserved4,  pl031_reg_map->Reserved4);
    printk("RTCPeriphID0: %x(%b)\n", & pl031_reg_map->RTCPeriphID0,  pl031_reg_map->RTCPeriphID0);
    printk("RTCPeriphID1: %x(%b)\n", & pl031_reg_map->RTCPeriphID1,  pl031_reg_map->RTCPeriphID1);
    printk("RTCPeriphID2: %x(%b)\n", & pl031_reg_map->RTCPeriphID2,  pl031_reg_map->RTCPeriphID2);
    printk("RTCPeriphID3: %x(%b)\n", & pl031_reg_map->RTCPeriphID3,  pl031_reg_map->RTCPeriphID3);
    printk("RTCPCellID0: %x(%b)\n", & pl031_reg_map->RTCPCellID0,  pl031_reg_map->RTCPCellID0);
    printk("RTCPCellID1: %x(%b)\n", & pl031_reg_map->RTCPCellID1,  pl031_reg_map->RTCPCellID1);
    printk("RTCPCellID2: %x(%b)\n", & pl031_reg_map->RTCPCellID2,  pl031_reg_map->RTCPCellID2);
    printk("RTCPCellID3: %x(%b)\n", & pl031_reg_map->RTCPCellID3,  pl031_reg_map->RTCPCellID3);
    printk("------ end  pl031 -----\n");
}